<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

/**
 * @intro return json
 * */
function ajax_return( $state = 0 , $message = '' , $data = [] , $to_arr = true )
{
    if($state === 302)
        $message = 'Please enter relevant parameters';
    elseif($state === 303)
        $message = 'no file upload';
    elseif($state === 304)
        $message = 'file upload failed';
    elseif($state === 403)
        $message = 'this page is not find!';
    elseif($state === 200)
        $message = $message?$message:'successful';
    elseif($state === 201)
        $message = 'no relevant data';
    elseif($state === 500)
        $message = 'insert failed';
    elseif($state === 501)
        $message = 'modify failed';
    elseif($state === 502)
        $message = 'delete failed';
    if($to_arr)
    {
        return json([ 'status' => $state, 'message' => $message, 'result' => data_trans( $data ) ]);
    }
    else
    {
        return json([ 'status' => $state, 'message' => $message, 'result' => $data ]);
    }
}
/**
 * $intro obj to array
 * @param $object import object
 * @return array
 * */
function data_trans( $object )
{
    return json_decode(json_encode($object),true);
}

/**
 * @brief 密码加密
 * @param $string string 要加密的字符串
 * @return $array 返回加密后的结果;
 * */
function encryption( $string )
{
    return sha1( md5( base64_encode( sha1($string , 'hash') ) ) );
}
/**
 *
 * */
function curl_get($url)
{
    $user_agent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0 );
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_USERAGENT, $user_agent); // 模拟用户使用的浏览器
    curl_setopt($ch, CURLOPT_AUTOREFERER, true);
    curl_setopt($ch, CURLOPT_REFERER, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}

function down_file($url = '')
{
    $date=date("Ymd-H:i:m");
    Header( "Content-type:  application/octet-stream ");
    Header( "Accept-Ranges:  bytes ");
    Header( "Accept-Length: " .filesize($url));
    header( "Content-Disposition:  attachment;  filename= {$date}.doc");
    echo file_get_contents($url);
    readfile($url);
}
